<?php !defined( 'SUNCMS' ) && exit( 'No direct script access allowed'  );

/**
 * 
 * Sun CMS
 * 
 * @author			Burning
 * @copyright		Copyright (c) 2009, IsunCms.Com.
 * @license			http://www.isuncms.com/license.htm
 * @link				http://www.isuncms.com/
 * @since				Version 1.0 Beta
 * 
 */

class Filemag{

	var $db;
	
	function __construct()
	{
		global $db;
		$this->db = $db;
	}
	
	function get_file_no( $type, $path = false )
	{
		if( $path ) return $this->db->count( DB_PRE.'images', array( 'path' => $path ) );
		return $this->db->count( DB_PRE.$type );
	}
	
	function get_file_list( $start, $type, $path = false )
	{
		$end = $type == 'images'?10:8;
		$where = $path?"WHERE `path`= '$path'":'';
		$item = $path?"`name`, `type`, `path`":( $type == 'flash'?"`id`, `name`, `size`":"`id`, `name`, `type`, `size`" );
		$sql = "SELECT $item FROM `".DB_PRE."$type` $where ORDER BY `id` DESC LIMIT $start, $end";
		$result = $this->db->query( $sql );
		foreach( $result as $k => $v )
		{
			if( $type == 'images' )
			{
				$result[$k]['url'] = SITE_URL.'uploads/'.$v['path'].'/'.$v['name'].'.'.$v['type'];
			}
			elseif( $type == 'flash' )
			{
				$result[$k]['url'] = SITE_URL.'uploads/'.$type.'/'.$v['name'].'.swf';
			}
			elseif( $type == 'media' )
			{
				$result[$k]['url'] = SITE_URL.'uploads/'.$type.'/'.$v['name'].'.'.$v['type'];
			}
			elseif( $type == 'enclosure' )
			{
				$result[$k]['url'] = SITE_URL.'download.php?file='.authcode( $v['name'], 'ENCODE' );
			}
		}
		return $result;
	}
	
	function get_emoticons_category()
	{
		$sql = "SELECT `path`, `name` FROM `".DB_PRE."emoticons` WHERE `type` = 0 ORDER by `id` ASC";
		$category = $this->db->query( $sql );
		return $category;
	}
	
	function get_emoticons_list( $cat )
	{
		$sql = "SELECT `id`, `path`, `description` FROM `".DB_PRE."emoticons` WHERE `type` = ( SELECT `id` FROM `".DB_PRE."emoticons` WHERE `path` = '$cat' ) ORDER by `order` ASC";
		$emoticons = $this->db->query( $sql );
		foreach( $emoticons as $k => $v )
		{
			$emoticons[$k]['url'] = SITE_URL.'images/emoticons/'.$cat.'/'.$v['path'];
			unset( $emoticons[$k]['path'] );
		}
		return $emoticons;
	}
	
}